package com.nq.service.impl;

/**

 * 开发者----股牛网络

 * 联系TG：gncms

 */


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nq.common.ServerResponse;

import com.nq.dao.UserBankMapper;

import com.nq.dao.UserUsdtMapper;
import com.nq.pojo.User;

import com.nq.pojo.UserBank;

import com.nq.pojo.UserUsdt;
import com.nq.pojo.vo.UserBankAndUsdtVo;
import com.nq.service.IUserBankService;

import com.nq.service.IUserService;


import com.nq.vo.user.UserBankInfoVO;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("iUserBankService")
 public class UserBankServiceImpl implements IUserBankService {

   @Autowired
   UserBankMapper userBankMapper;
   @Autowired
   private UserUsdtMapper userUsdtMapper;

   @Autowired
   IUserService iUserService;

   public UserBank findUserBankByUserId(Integer userId) { return this.userBankMapper.findUserBankByUserId(userId); }

  @Transactional
  @Override
  public ServerResponse addBank(UserBank bank, HttpServletRequest request) {

     User user = this.iUserService.getCurrentRefreshUser(request);
     if (user == null ){
       return ServerResponse.createBySuccessMsg("Please log in first.");
     }

     UserBank dbBank = this.userBankMapper.findUserBankByUserId(user.getId());

      UserBank userBank = new UserBank();
      userBank.setUserId(user.getId());
      userBank.setBankName(bank.getBankName());
      userBank.setBankNo(bank.getBankNo());
      userBank.setBankAddress(bank.getBankAddress());
      userBank.setBankImg(bank.getBankImg());
      userBank.setBankPhone(bank.getBankPhone());
      userBank.setAddTime(new Date());
      userBank.setUsdtAddr(bank.getUsdtAddr());
      userBank.setCountry(bank.getCountry());
      userBank.setIfscNo(bank.getIfscNo());
     if (dbBank != null) {
         userBank.setId(dbBank.getId());
         this.userBankMapper.updateById(userBank);
         return ServerResponse.createBySuccess("Successfully modified the bank card");
     }

     int insertCount = this.userBankMapper.insert(userBank);
     if (insertCount > 0) {
       return ServerResponse.createBySuccess("Successfully added the bank card");
     }
     return ServerResponse.createByErrorMsg("Failed to add bank card");
   }

   @Transactional
   public ServerResponse updateBank(UserBank bank, HttpServletRequest request) {

     User user = this.iUserService.getCurrentRefreshUser(request);

     UserBank dbBank = this.userBankMapper.findUserBankByUserId(user.getId());

     if (dbBank == null) {

       return ServerResponse.createByErrorMsg("The modification failed and the bank could not be found.");

     }

     dbBank.setBankName(bank.getBankName());

     dbBank.setBankNo(bank.getBankNo());

     dbBank.setBankAddress(bank.getBankAddress());

     dbBank.setBankImg(bank.getBankImg());

     dbBank.setBankPhone(bank.getBankPhone());

     dbBank.setUsdtAddr(bank.getUsdtAddr());

     dbBank.setIfscNo(bank.getIfscNo());

     dbBank.setCountry(bank.getCountry());

     int updateCount = this.userBankMapper.updateByPrimaryKeySelective(dbBank);

     if (updateCount > 0) {

       return ServerResponse.createBySuccess("Successfully modified the bank card.");

     }

     return ServerResponse.createByErrorMsg("Failed to modify the bank card.");
   }

   public ServerResponse getBankInfo(HttpServletRequest request) {

     User user = this.iUserService.getCurrentRefreshUser(request);

     UserBank dbBank = this.userBankMapper.findUserBankByUserId(user.getId());

     if (dbBank == null) {

       return ServerResponse.createByErrorMsg("No bank information has been added.");

     }

     UserBankInfoVO userBankInfoVO = new UserBankInfoVO();

     userBankInfoVO.setRealName(user.getRealName());

     userBankInfoVO.setBankName(dbBank.getBankName());

     userBankInfoVO.setBankAddress(dbBank.getBankAddress());

     userBankInfoVO.setBankNo(dbBank.getBankNo());

     userBankInfoVO.setUsdtAddr(dbBank.getUsdtAddr());

     userBankInfoVO.setCountry(dbBank.getCountry());

     userBankInfoVO.setIfscNo(dbBank.getIfscNo());

     return ServerResponse.createBySuccess(userBankInfoVO);

   }

   public ServerResponse updateBankByAdmin(UserBank userBank) {

     if (userBank.getId() == null) {

       return ServerResponse.createByErrorMsg("Modify-id must be sent");

     }

     int updateCount = this.userBankMapper.updateByPrimaryKeySelective(userBank);

     if (updateCount > 0) {

       return ServerResponse.createBySuccessMsg("Successful modification");

     }

     return ServerResponse.createByErrorMsg("Modification Failed");

   }

   public ServerResponse getBank(Integer userId) {

     // 查询银行卡信息
     QueryWrapper<UserBank> bankQueryWrapper = new QueryWrapper<>();
     bankQueryWrapper.eq("user_id", userId);
     UserBank bank = this.userBankMapper.selectOne(bankQueryWrapper);

     // 查询usdt
     UserUsdt usdt = this.userUsdtMapper.findUserUsdtByUserId(userId);
     if (null != bank && null != usdt) {
       bank.setUsdtAddr(usdt.getUsdtAddr());
     }
     if (null == bank) {
       bank = new UserBank();
     }
     bank.setUserId(userId);
     return ServerResponse.createBySuccess(bank);
   }

   @Override
   @Transactional
   public int updateBankByUserId(UserBank userBank) {

     // 包含usdt和银行卡信息
     Integer userId = userBank.getUserId();
     QueryWrapper<UserBank> bankQueryWrapper = new QueryWrapper<>();
     bankQueryWrapper.eq("user_id", userId);
     UserBank bank = this.userBankMapper.selectOne(bankQueryWrapper);
     if (null == bank) {

       userBank.setAddTime(new Date());
       userBankMapper.insert(userBank);
     }else {
       UserBank updateBank = new UserBank();
       BeanUtils.copyProperties(userBank, updateBank);
       updateBank.setId(bank.getId());
       userBankMapper.updateById(updateBank);
     }

     // 新增或更新usdt
     UserUsdt usdt = this.userUsdtMapper.findUserUsdtByUserId(userId);
     if (null == usdt) {
       UserUsdt userUsdt = new UserUsdt();
       userUsdt.setUserId(userId);
       userUsdt.setNetType(1);
       userUsdt.setUsdtAddr(userBank.getUsdtAddr());
       userUsdt.setCreateTime(new Date());
       userUsdtMapper.insertSelective(userUsdt);
     }else {
       usdt.setNetType(1);
       usdt.setUsdtAddr(userBank.getUsdtAddr());
       userUsdtMapper.updateByPrimaryKeySelective(usdt);
     }

     return 1;
   }

    @Override
    public int deleteUserBank(Integer userId) {
        return userBankMapper.deleteByUserId(userId);
    }

}
